package main

import (
	"flag"
	"goruntimeDistributionCrawler/config"
	"goruntimeDistributionCrawler/engine"
	"goruntimeDistributionCrawler/persist/client"
	"goruntimeDistributionCrawler/scheduler"
	"goruntimeDistributionCrawler/workclient"
	"goruntimeDistributionCrawler/zhenai/parser"
	"net/rpc"
	"strings"
)

var url="http://www.zhenai.com/zhenghun"
//var url="http://m.zhenai.com/zhenghun/dadukou"
//var url="http://m.zhenai.com/u/1775915389#seo"
var ipList string
func init()  {
	flag.StringVar(&ipList,"ipList","0","input iplist")
}

func main() {
	flag.Parse()
	if ipList =="0"{
		panic("error")
	}
	workClientChan:=make(chan *rpc.Client)
	createWorkClient(ipList,workClientChan)
	//将请求 和 执行单元 放到队列中进行执行
	itemChan,_:=client.ItemSaver(config.SAVE_HOST_PROT)

	e:=engine.ConcurrentEngine{
		  Scheduler:&scheduler.QueuedScheduler{},
	      WorkerCount:100,
		  ItemChan:itemChan,
		  WorkerManger:workclient.Workclient(workClientChan),
	}

	//多个执行单元抢一个request
	//e:=engine.ConcurrentEngine{
	//	Scheduler:&scheduler.SimpleScheduler{},
	//    WorkerCount:100,
	//}
	e.Run(
		  engine.Request{
		  	UrL:url,
		  	Parser:engine.NewParserFunc(parser.PaseCityList,"PaseCityList"),
		  },
		)
}

func createWorkClient(ip string,workClientChan chan *rpc.Client)  {
	ipList:=strings.Split(ip,"@")
	if len(ipList) <=0{
		panic("输入端口地址")
	}
	for _,v:=range ipList{
		for  {
			go func(v string) {
				client,err:=workclient.CreateClient(v)
				if err ==nil {
					workClientChan<-client
				}
			}(v)
		}
	}

}

